package pers.lan.stack;

import java.util.List;

/**
 * @author <p><a href="mailto:1728209643@qq.com">XiaoSan</a>
 * @date 2019/9/6 10:37 AM
 */
public class LC_385 {

    public NestedInteger deserialize(String s) {
        if (s.charAt(0) != '[') {
            return new NestedInteger(Integer.parseInt(s));
        } else {
            NestedInteger result = new NestedInteger();
            int index = -1, cnt = 0;
            for (int i = 1; i < s.length() - 1; ++i) {
                char ch = s.charAt(i);
                if (ch == '[') {
                    if (index == -1) {
                        index = i;
                    }
                    cnt++;
                } else if (ch == ']') {
                    cnt--;
                } else if (ch == ',') {
                    if (cnt > 0) {
                        continue;
                    }
                    result.add(deserialize(s.substring(index, i)));
                    index = -1;
                } else {
                    if (index == -1) {
                        index = i;
                    }
                }
            }
            if (index != -1) {
                result.add(deserialize(s.substring(index, s.length() - 1)));
            }
            return result;
        }
    }

    class NestedInteger {
        public NestedInteger() {

        }

        public NestedInteger(int value) {

        }

        public boolean isInteger() {
            return true;
        }

        public Integer getInteger() {
            return null;
        }

        public void setInteger(int value) {

        }

        public void add(NestedInteger ni) {

        }

        public List<NestedInteger> getList() {
            return null;
        }
    }
}
